package com.wwy;

import com.wwy.domain.TypeEntity;
import com.wwy.listener.MenuActionListener;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: wwy
 * @Date: 2020/8/13 12:29
 */
public class SwingArea extends JFrame {
    private static SwingArea instance = null;
    private JProgressBar progressBar;
    private MenuActionListener menuActionListener = new MenuActionListener();
    public JPanel jPanel;
    /**
     * sping容器对象
     */
    public static ApplicationContext applicationContext;
    private SwingArea() {
    }
    public static SwingArea getInstance() {
        if (null == instance) {
            synchronized (SwingArea.class) {
                if (null == instance) {
                    instance = new SwingArea();
                }
            }
        }
        return instance;
    }
    public void initUI() {
        this.setTitle("MQTT数据上传测试");
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 1280, 800);
        this.setLayout(null);
        //创建菜单
        this.createMenu();
        jPanel = new JPanel();
        jPanel.setPreferredSize(new Dimension(100,6000));
        jPanel.setBackground(Color.PINK);
        JScrollPane jScrollPane = new JScrollPane(jPanel);
        jScrollPane.setBounds(0, 0, 1230, 800);
        this.setContentPane(jScrollPane);

        setVisible(true);

    }

    public void creatLabelTable(TypeEntity typeEntity){
        if(typeEntity ==null){
            return;
        }
        LabelPanel labelPanel = new LabelPanel(typeEntity);
        jPanel.add(labelPanel);
        jPanel.revalidate();
    }

    /**
     * 创建菜单
     */
    public void createMenu(){
        //创建菜单
        JMenu jMenu = new JMenu("编辑");
        addItem(jMenu,"新建类型","newType",menuActionListener);
        addItem(jMenu,"新建标签","newLabel",menuActionListener);
        JMenuBar jMenuBar = new JMenuBar();
        jMenuBar.add(jMenu);

        JMenu jMenu1 = new JMenu("操纵");
        addItem(jMenu1,"开始","start",menuActionListener);
        addItem(jMenu1,"关闭","close",menuActionListener);
        addItem(jMenu1,"清屏","clear",menuActionListener);
        jMenuBar.add(jMenu1);
        JMenu jMenu2 = new JMenu("设置");
        addItem(jMenu2,"阀值设置","rssi",menuActionListener);
        addItem(jMenu2,"过滤时间设置","filtrationTime",menuActionListener);
        jMenuBar.add(jMenu2);
        this.setJMenuBar(jMenuBar);
    }

    /**
     * 创建lable类型
     */
    public void createLabelTypeDialog(){
        TypeDialog typeDialog = new TypeDialog(this,"新建类型");
        typeDialog.setVisible(true);
    }
    /**
     * 新增lable
     */
    public void createLabelDialog(){
        LabelDialog dialog = new LabelDialog(this, "新建标签");
        dialog.setVisible(true);
    }

    public void createRssiDialog(){
        RssiDialog dialog = new RssiDialog(this,"阀值设置");
        dialog.setVisible(true);
    }

    /**
     * 过滤时间设置
     */
    public void createFiltrationTimeDialog(){
        FiltrationTimeDialog filtrationTimeDialog = new FiltrationTimeDialog(this,"时间范围测试");
        filtrationTimeDialog.setVisible(true);
    }

    private void addItem(JMenu Menu,String menuName,String code,ActionListener listener){
        JMenuItem anItem = new JMenuItem(menuName);
        anItem.setActionCommand(code);
        anItem.addActionListener(listener);
        Menu.add(anItem);
    }

}
